package com.och.mrcp.handler;

import com.och.mrcp.core.mrcp.model.MrcpMessage;
import com.och.mrcp.core.rtp.session.RtpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * MRCP消息发送器
 * 负责将MRCP消息发送回客户端
 */
@Slf4j
@Component
public class MrcpMessageSender {
    
    // 会话回调映射
    private final ConcurrentMap<String, MrcpMessageCallback> sessionCallbacks = new ConcurrentHashMap<>();
    
    /**
     * 注册会话回调
     */
    public void registerCallback(String sessionId, MrcpMessageCallback callback) {
        sessionCallbacks.put(sessionId, callback);
        log.debug("Registered MRCP message callback for session: {}", sessionId);
    }
    
    /**
     * 注销会话回调
     */
    public void unregisterCallback(String sessionId) {
        sessionCallbacks.remove(sessionId);
        log.debug("Unregistered MRCP message callback for session: {}", sessionId);
    }
    
    /**
     * 发送MRCP消息
     */
    public boolean sendMessage(String sessionId, MrcpMessage message) {
        MrcpMessageCallback callback = sessionCallbacks.get(sessionId);
        if (callback != null) {
            try {
                callback.onMrcpMessage(message);
                log.debug("MRCP message sent for session {}: {}", sessionId, message.getMessageTypeDescription());
                return true;
            } catch (Exception e) {
                log.error("Error sending MRCP message for session {}: {}", sessionId, e.getMessage(), e);
                return false;
            }
        } else {
            log.warn("No callback registered for session: {}", sessionId);
            return false;
        }
    }
    
    /**
     * 发送MRCP消息（通过RTP会话）
     */
    public boolean sendMessage(RtpSession rtpSession, MrcpMessage message) {
        return sendMessage(rtpSession.getSessionId(), message);
    }
    
    /**
     * 广播消息到所有会话
     */
    public void broadcastMessage(MrcpMessage message) {
        sessionCallbacks.forEach((sessionId, callback) -> {
            try {
                callback.onMrcpMessage(message);
                log.debug("Broadcast MRCP message sent to session: {}", sessionId);
            } catch (Exception e) {
                log.error("Error broadcasting MRCP message to session {}: {}", sessionId, e.getMessage(), e);
            }
        });
    }
    
    /**
     * 检查会话是否有回调
     */
    public boolean hasCallback(String sessionId) {
        return sessionCallbacks.containsKey(sessionId);
    }
    
    /**
     * 获取活跃会话数量
     */
    public int getActiveSessionCount() {
        return sessionCallbacks.size();
    }
    
    /**
     * 清理所有回调
     */
    public void cleanup() {
        sessionCallbacks.clear();
        log.info("Cleaned up all MRCP message callbacks");
    }
    
    /**
     * MRCP消息回调接口
     */
    public interface MrcpMessageCallback {
        /**
         * 当需要发送MRCP消息时调用
         * @param message MRCP消息
         */
        void onMrcpMessage(MrcpMessage message);
    }
}
